package
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;

	[SWF(width="800", height="600", frameRate="40", backgroundColor="#414647")]
	public class box2dTest extends Sprite
	{
		
		public var m_world:b2World;
		
		[Embed("../img/block.jpg")]
		private var Block:Class;

		
		public function box2dTest()
		{
			init();
//			var block:DisplayObject = new Block();
		}
		
		private function init():void
		{
			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
			
			// Construct a world object
			m_world = new b2World(gravity, true);
			
			// set debug draw
			var debugDraw:b2DebugDraw = new b2DebugDraw();
			debugDraw.SetSprite(this);
			debugDraw.SetDrawScale(30.0);
			debugDraw.SetFillAlpha(0.3);
			debugDraw.SetLineThickness(1.0);
			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			m_world.SetDebugDraw(debugDraw);
			m_world.DrawDebugData();
			
			// Vars used to create bodies
			var body:b2Body;
			var bodyDef:b2BodyDef;
			var boxShape:b2PolygonShape;
			var circleShape:b2CircleShape;
			
			// Add ground body
			bodyDef = new b2BodyDef();
//			bodyDef.position.Set(15, 19);
			bodyDef.position.Set(10, 12);
			//bodyDef.angle = 0.1;
			boxShape = new b2PolygonShape();
			boxShape.SetAsBox(30, 3);
			
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = boxShape;
			fixtureDef.friction = 0.3;
			fixtureDef.density = 0; // static bodies require zero density
			
			// Add sprite to body userData
			var sprite:Sprite = new Sprite();
			sprite.addChild(doDrawRect(550, 120));
			bodyDef.userData = sprite;
			bodyDef.userData.y = 330;
//			bodyDef.userData.width = 30 * 2 * 30; 
//			bodyDef.userData.height = 30 * 2 * 3;
			addChild(bodyDef.userData);
			body = m_world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			
			// Add some objects
			bodyDef = new b2BodyDef();
			bodyDef.position.x = 150;
			bodyDef.position.y = 150;

			boxShape = new b2PolygonShape();
			boxShape.SetAsBox(1, 1);
			fixtureDef.shape = boxShape;
			fixtureDef.density = 1.0;
			fixtureDef.friction = 0.5;
			fixtureDef.restitution = 0.2;
			
			sprite = new Sprite();
			sprite.addChild(doDrawRect(50, 80));
			bodyDef.userData = sprite;
			bodyDef.userData.width = 60; 
			bodyDef.userData.height = 60; 
			body = m_world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			
			addChild(bodyDef.userData);
		}
		
		private function doDrawRect(width:uint, height:uint, bgColor:uint = 0x9966CC, borderColor:uint = 0xffffff):Shape 
		{
			var child:Shape = new Shape();
			child.graphics.beginFill(bgColor);
			child.graphics.lineStyle(1, borderColor);
			child.graphics.drawRect(0, 0, width, height);
			child.graphics.endFill();
			return child;
		}
	}
}